// pages/consultantapply/consultantapply.js
var app = getApp()
const $api = require('../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示显示    0表示不显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '商品列表', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    height: app.globalData.height,
    applyData: {},
    state: 1,
    addressSHQ: ['省', '市', '区'],
    addressList: [0, 0, 0], //地址省市区默认下标
    addressSHQ: ['省', '市', '区'],
    regionIndex: [0, 0, 0]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    //获取默认省
    $api.getArea({
      login_token: wx.getStorageSync('login_token'),
      level: 1
    }).then((res) => {
      _this.setData({
        addressJson: res.data.data,
        loadModal: false
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  onInput(e) {
    console.log(e)
    let applyData = this.data.applyData
    let value = e.detail.value
    let type = e.target.dataset.type
    applyData[type] = value
    this.setData({ applyData })
  },
  showPicker() {
    console.log(123)
    this.setData({
      showPicker: true
    });
  },

  onPickerConfirm(e) {
    const { year, month, period, periodText } = e.detail;

    this.setData({
      selectedPeriod: {
        year,
        month,
        period,
        periodText
      },
      showPicker: false,
      apply_zd_time: year + '年' + month + '月' + periodText
    });

    console.log('选择的时间段:', e.detail);
  },

  onPickerClose() {
    this.setData({
      showPicker: false
    });
  },
  out() {
    wx.navigateBack({
      delta: 1,
    })
  },
  upgo() {
    if (this.data.state == 1) {
      let applyData = this.data.applyData
      let apply_zd_time = this.data.apply_zd_time
      if (!applyData.name || !applyData.mobile || !applyData.address || !apply_zd_time) {
        wx.showToast({
          title: '请将信息填写完整',
          icon: 'none',
          duration: 2000
        })
        return
      }
      let province_id = this.data.province_id
      let city_id = this.data.city_id
      let area_id = this.data.area_id
      if(!province_id||!city_id||!area_id){
        wx.showToast({
          title: '请选择驻店地址',
          icon: 'none',
          duration: 2000
        })
        return
      }
      this.setData({
        state: 2
      })
    } else {
      this.setData({
        state: 1
      })
    }

  },
  upsum() {
    let _this = this
    let applyData = _this.data.applyData
    let apply_zd_time = _this.data.apply_zd_time
    let province_id = _this.data.province_id
      let city_id = _this.data.city_id
      let area_id = _this.data.area_id
    $api.applygwzdinfo({
      login_token: wx.getStorageSync('login_token'),
      name: applyData.name,
      mobile: applyData.mobile,
      address: applyData.address,
      apply_zd_time: apply_zd_time,
      province_id:province_id,
      city_id:city_id,
      area_id:area_id,
    }).then((res) => {
      if (res.data.code == 0) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        }, 1000)

      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }).catch((err) => {

    })
  },
  //地点底部弹出
  onSHG(e) {
    var _this = this;
    _this.setData({
      // loadModal: true,
      modalName: e.currentTarget.dataset.target,
    })
  },
  //滑动时获取地址index
  onAddressIndex(e) {
    console.log(e)
    var _this = this;
    _this.setData({
      regionIndex: e.detail.value
    })
  },
  //确认地址
  confirmAddress() {
    var _this = this;
    var regionIndex = _this.data.regionIndex;
    _this.setData({
      ['addressSHQ[0]']: _this.data.addressJson[regionIndex[0]].provinceName,
      province_id: _this.data.addressJson[regionIndex[0]].provinceId,
      ['addressSHQ[1]']: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].cityName,
      city_id: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].cityId,
      ['addressSHQ[2]']: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].region[regionIndex[2]].regionName,
      area_id: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].region[regionIndex[2]].regionId,
      modalName: null,
      regionIndex: [0, 0, 0]
    })
  },
})